package project.data;

import java.util.ArrayList;
import java.util.List;

public class FeasibilityDataStack implements EvaporableDataHolder{
	private List<FeasibilityData> feasDataStackPickUp = new ArrayList<FeasibilityData>();
	private List<FeasibilityData> feasDataStackDropOff = new ArrayList<FeasibilityData>();

	public FeasibilityDataStack(){
		
	}
	
	public void add(FeasibilityData data){
		if(data.hasEvaporated()){
			throw new IllegalArgumentException();
		}
		else{
			if(data.isFromPickUpPoint()){
				feasDataStackPickUp.add(data);
			}
			else{
				feasDataStackDropOff.add(data);
			}
		}
	}
	
	@Override
	public void remove(EvaporableData evaporatedData) {
		FeasibilityData evapData = (FeasibilityData)evaporatedData;
		if(evapData.isFromPickUpPoint()){
			feasDataStackPickUp.remove(evapData);
		}
		else{
			feasDataStackDropOff.remove(evapData);
		}
		
	}
	
	
	
	/**
	 * Returns the Drop Off FeasibilityData that matches the packageID 
	 */
	public FeasibilityData getDropOffFeasiblityDataFromPackage(int packageID){
		for(FeasibilityData data: feasDataStackDropOff){
			if(data.getOriginPackageId() == packageID){
				return data;
			}
		}
		return null;
	}
	
	public double getHighestPickUpPhermoneCount(List<Integer> destinationIDsToBeIgnored){
		double result = 0;
		for(FeasibilityData data: feasDataStackPickUp){
			if(result == 0 && !destinationIDsToBeIgnored.contains((Integer)data.getOriginPackageId())){
				result = data.getRemainingSmell();
			}
			else{
				if(data.getRemainingSmell() > result && !destinationIDsToBeIgnored.contains((Integer)data.getOriginPackageId())){
					result = data.getRemainingSmell();
				}
			}
		}
		return result;
	}
	
	public double getRemainingSmellFromPackage(int packageID){
		for(FeasibilityData data: feasDataStackPickUp){
			if(data.getOriginPackageId() == packageID){
//				System.out.println("[FeasibilityDataStack] remaining smell: "+ data.getRemainingSmell());
				return data.getRemainingSmell();
			}
		}
		return 0;
	}
	
	public void dropPheromones(int packageId, boolean isFromPickUpPoint, double pheromoneValue){
		
		if(isFromPickUpPoint){
//			System.out.println("dropphero");
			FeasibilityData data = getPickUpFeasiblityDataFromPackage(packageId);
			if(data == null){
				data = new FeasibilityData(packageId, this, isFromPickUpPoint);
				feasDataStackPickUp.add(data);
			}

			data.dropEvaporableData(pheromoneValue);
//				System.out.println("Data Dropped -------------------------");

		}
		else{
//			System.out.println("dropphero");
			FeasibilityData data = getDropOffFeasiblityDataFromPackage(packageId);
			if(data == null){
				data = new FeasibilityData(packageId, this, isFromPickUpPoint);
				feasDataStackDropOff.add(data);
			}
				data.dropEvaporableData(pheromoneValue);
//				System.out.println("Data Dropped -------------------------");
		}
	}

	private FeasibilityData getPickUpFeasiblityDataFromPackage(int packageId) {
		for(FeasibilityData data: feasDataStackPickUp){
			if(data.getOriginPackageId() == packageId){
				return data;
			}
		}
		return null;
	}

	public List<FeasibilityData> getFeasDataPickUp() {
		return feasDataStackPickUp;
	}
	
	
	
	
}
